﻿namespace DelegateDemo001
{
    internal class Program
    {
        delegate string GetAString();
        static void Main(string[] args)
        {
            var dealer = new CarDealer();
            var valtteri = new Consumer("Valtteri");
            dealer.NewCarInfo += valtteri.NewCarIsHear;
            dealer.NewCar("Williams");

            Console.WriteLine("Hello, World!");
            int x = 40;
            GetAString getAString = new GetAString(x.ToString);
            Console.WriteLine(getAString());

            GetAString getAString1 = x.ToString;
            Console.WriteLine(getAString1());

            GetAString getAString2 = delegate () { return "Hello, Delegate!"; };
            Console.WriteLine(getAString2());

            GetAString getAString3 = () => "Hello, Lambda!";
            Console.WriteLine(getAString3());

            GetAString getAString4 = () => "Hello, Lambda!";
            Console.WriteLine(getAString3());
        }
    }

    public class CarInfoEventArgs : EventArgs
    {
        public string Car { get; }
        public CarInfoEventArgs(string car)
        {
            Car = car;
        }
    }

    public class CarDealer
    {
        public event EventHandler<CarInfoEventArgs> NewCarInfo;

        public void NewCar(string car)
        {
            Console.WriteLine($"cARdEALER,NEW CAR {car}");
            NewCarInfo?.Invoke(this, new CarInfoEventArgs(car) );
        }
    }

    public class Consumer
    {
        private string _name;
        public Consumer(string name)
        {
            _name = name;
        }

        public void NewCarIsHear(object sender, CarInfoEventArgs e)
        {
            Console.WriteLine($"{_name}:car {e.Car} is new ");
        }
    }
}
 